home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / DefaultTableCellRenderer.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  140 lines

  1. /*
  2.  * @(#)DefaultTableCellRenderer.java    1.9 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.table;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.table.TableCellRenderer;
  25. import com.sun.java.swing.border.*;
  26.  
  27. import java.awt.Component;
  28. import java.awt.Color;
  29.  
  30. import java.io.Serializable;
  31.  
  32. /**
  33.  * The standard class for rendering (displaying) individual cells
  34.  * in a JTable.
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @version 1.9 02/02/98
  44.  * @author Philip Milne 
  45.  * @see JTable
  46.  */
  47. public class DefaultTableCellRenderer extends JLabel
  48.     implements TableCellRenderer, Serializable
  49. {
  50.  
  51.     protected static Border noFocusBorder; 
  52.     
  53.     // We need a place to store the color the JLabel should be returned 
  54.     // to after its foreground and background colors have been set 
  55.     // to the selection background color. 
  56.     // These ivars will be made protected when their names are finalized. 
  57.     private Color unselectedForeground; 
  58.     private Color unselectedBackground; 
  59.  
  60.     public DefaultTableCellRenderer() {
  61.     super();
  62.         noFocusBorder = new EmptyBorder(1, 2, 1, 2);
  63.     setOpaque(true);
  64.         setBorder(noFocusBorder);
  65.     }
  66.  
  67.     public void setForeground(Color c) {
  68.         super.setForeground(c); 
  69.         unselectedForeground = c; 
  70.     }
  71.     
  72.     public void setBackground(Color c) {
  73.         super.setBackground(c); 
  74.         unselectedBackground = c; 
  75.     }
  76.  
  77.     public void updateUI() {
  78.         super.updateUI(); 
  79.     setForeground(null);
  80.     setBackground(null);
  81.     }
  82.     
  83.     public Component getTableCellRendererComponent(JTable table, Object value,
  84.                           boolean isSelected, boolean hasFocus, int row, int column) {
  85.  
  86.     if (isSelected) {
  87.        super.setForeground(table.getSelectionForeground());
  88.        super.setBackground(table.getSelectionBackground());
  89.     }
  90.     else {
  91.         super.setForeground((unselectedForeground != null) ? unselectedForeground 
  92.                                                            : table.getForeground());
  93.         super.setBackground((unselectedBackground != null) ? unselectedBackground 
  94.                                                            : table.getBackground());
  95.     }
  96.     
  97.     setFont(table.getFont());
  98.  
  99.     if (hasFocus) {
  100.         setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
  101.         if (table.isCellEditable(row, column)) {
  102.             super.setForeground( UIManager.getColor("Table.focusCellForeground") );
  103.             super.setBackground( UIManager.getColor("Table.focusCellBackground") );
  104.         }
  105.     } else {
  106.         setBorder(noFocusBorder);
  107.     }
  108.  
  109.         setValue(value); 
  110.         
  111.     return this;
  112.     }
  113.     
  114.     protected void setValue(Object value) {
  115.     setText((value == null) ? "" : value.toString());
  116.     }
  117.  
  118.  
  119.     /**
  120.      * A subclass of BasicTableCellRenderer that implements UIResource.
  121.      * BasicTableCellRenderer doesn't implement UIResource
  122.      * directly so that applications can safely override the
  123.      * cellRenderer property with BasicTableCellRenderer subclasses.
  124.      * <p>
  125.      * Warning: serialized objects of this class will not be compatible with
  126.      * future swing releases.  The current serialization support is appropriate
  127.      * for short term storage or RMI between Swing1.0 applications.  It will
  128.      * not be possible to load serialized Swing1.0 objects with future releases
  129.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  130.      * baseline for the serialized form of Swing objects.
  131.      */
  132.     public static class UIResource extends DefaultTableCellRenderer 
  133.         implements com.sun.java.swing.plaf.UIResource
  134.     {
  135.     }
  136.  
  137. }
  138.  
  139.  
  140.